import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paly_android/bean/article_data_entity.dart';
import 'package:paly_android/bloc/wx_article_bloc.dart';
import 'package:paly_android/route/routes.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class WxArticlePage extends StatefulWidget {
  String typeId = "";

  WxArticlePage(this.typeId, {Key? key}) : super(key: key);

  @override
  _WxArticlePageState createState() {
    return _WxArticlePageState(typeId);
  }
}

class _WxArticlePageState extends State<WxArticlePage>
    with AutomaticKeepAliveClientMixin {
  int _page = 0;

  String typeId = "";
  late WxArticleBloc _wxArticleBloc;

  _WxArticlePageState(this.typeId);

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  bool refresh = true;

  @override
  void initState() {
    super.initState();
    _wxArticleBloc = WxArticleBloc();
    _wxArticleBloc.add(WxOneTypeEvent(typeId.toString(), 1));
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _refreshPage() {
    refresh = true;
    _page = 0;
    _getArticles();
  }

  void _loadMorePage() {
    refresh = false;
    _page++;
    _getArticles();
  }

  void _getArticles() {
    _wxArticleBloc.add(WxOneTypeEvent(typeId.toString(), _page));
  }

  @override
  Widget build(BuildContext bcontext) {
    return BlocProvider<WxArticleBloc>(
      create: (BuildContext context) => _wxArticleBloc,
      child: BlocBuilder<WxArticleBloc, WxArticleState>(
        buildWhen: (pre, cur) {
          if (refresh) {
            _refreshController.refreshCompleted();
          } else {
            _refreshController.loadComplete();
          }
          return true;
        },
        builder: (context, state) {
          List<ArticleDataDatas> data = (state as WxArticleInitial).articles;

          return SmartRefresher(
            enablePullUp: true,
            onRefresh: _refreshPage,
            onLoading: _loadMorePage,
            controller: _refreshController,
            child: ListView.builder(
                itemCount: data.length,
                itemBuilder: (BuildContext context, int index) {
                  ArticleDataDatas bean = data[index];

                  return Card(
                    child: ListTile(
                      title: Text(bean.title,maxLines: 1, overflow: TextOverflow.ellipsis,),
                      dense: true,
                      trailing: Text(
                        DateUtil.formatDateMs(bean.publishTime,
                            format: DateFormats.y_mo_d),
                        style: TextStyle(fontSize: 10),
                      ),

                      onTap: (){
                        Navigator.pushNamed(bcontext, MyRouter.WEB,arguments: {"title":bean.title,"url":bean.link});
                      },
                    ),
                  );
                }),
          );
        },
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
